using Godot;
using System;

public class Playable : KinematicBody2D
{
    [Export] public int playerSpeed = 300;
    [Export] public int originalSpeed = 300;

    [Export] public int dashingSpeed = 520;

    public Vector2 velocity = new Vector2();

    SignReact signScript = new SignReact();

    AnimatedTexture signTexture = ResourceLoader
    .Load<AnimatedTexture>("res://Sprites/Interactables/Sign/sign.tres");

    Globals GameManager;
    Node dialogVars;

    Area2D interActable;

    public override void _Ready()
    {
        GameManager = (Globals)GetNode("/root/GameManager");
        dialogVars = GetNode("/root/Dialogue");
        interActable = (Area2D)GetNode("InteractableCheck");
    }

    public void GetInput()
    {
        //var diag = GetNode("/root/Dialogue");

        velocity = new Vector2();

        if (Input.IsActionPressed("ui_right") && GameManager.canMove) velocity.x += 1;

        if (Input.IsActionPressed("ui_left") && GameManager.canMove) velocity.x -= 1;

        if (GameManager.rpgMode)
        {
            rpgMovement();
        }
        else
        {
            platMovement();
        }

        // Restrict movement once there's a textbox popping up. Fix this as it will always execute.
        if ((bool)dialogVars.Get("is_printing_text") && !GameManager.canInteract)
        {
            GameManager.canMove = false;
        }
        else
        {
            GameManager.canMove = true;
        }

        velocity = velocity.Normalized() * playerSpeed;
    }
    public void platMovement()
    {
        //if (Input.IsActionJustPressed("platformer_jump")) //Jump

        //if (Input.IsActionPressed("ui_down")) // Fast fall
    }

    public void rpgMovement()
    {
        if (Input.IsActionPressed("ui_down") && GameManager.canMove) velocity.y += 1;

        if (Input.IsActionPressed("ui_up") && GameManager.canMove) velocity.y -= 1;

        if (Input.IsActionPressed("rpg_dash") && !GameManager.canInteract)
        {
            playerSpeed = dashingSpeed;
        }
        else
        {
            playerSpeed = originalSpeed;
        }
    }

    // React/animate stuff when player approaches something interactable.
    public void interactRangeBodyEntered(Node body)
    {
        if (body.IsInGroup("Interactable"))
        {
            GameManager.canInteract = true;


            if (body.IsInGroup("Sign1"))
            {
                signTexture.Pause = false;
                GameManager.playerNearSign1 = true;
            }
        }

        GD.Print("Approaching: Interactable" + ", canInteract: " + GameManager.canInteract);

    }

    public void interactRangeBodyExited(Node body)
    {
        GameManager.canInteract = false;

        GD.Print("Leaving: Interactable" + ", canInteract: " + GameManager.canInteract);
        GameManager.playerNearSign1 = false;
        signTexture.Pause = true;
        signTexture.CurrentFrame = 0;
    }


    // Called when the node enters the scene tree for the first time.


    public override void _Process(float delta)
    {
        GetInput();

        // Uses AnimationTree node and its BlendSpaces.
        AnimationTree myAnimTree = GetNode<AnimationTree>("AnimationTree");
        AnimationNodeStateMachinePlayback stateMachinePlayback = (AnimationNodeStateMachinePlayback)myAnimTree.Get("parameters/playback");

        // If there is no movement, trigger idle animation.
        if (velocity == Vector2.Zero)
            stateMachinePlayback.Travel("Idle");
        else
        {
            // Trigger "Walk" animation if there is movement.
            stateMachinePlayback.Travel("Movement");

            myAnimTree.Set("parameters/Idle/blend_position", velocity);

            myAnimTree.Set("parameters/Movement/blend_position", velocity);

            velocity = MoveAndSlide(velocity);
        }

    }
}
